package prac;

import java.util.*;

public class StudentScore {

    public static String type;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
         Map<Integer,String> subject=new HashMap<>(); //记录学科
        List<Socre> socreList=new ArrayList<>();
         int n = sc.nextInt();
         int m = sc.nextInt();
         for (int i = 0; i < m; i++) {
            String s = sc.next();
             subject.put(i,s);
             }

         for (int i = 0; i < n; i++) {
             Socre stu = new Socre();
            stu.name = sc.next();
            int sum = 0;
            for (int j = 0; j < m; j++) {
                 int score = sc.nextInt();
                 stu.socre.put(subject.get(j),score);
                 sum += score;
                 }
             stu.socre.put("zongfen",sum);
             socreList.add(stu);
            }
        type = sc.next();
        Collections.sort(socreList);
       for(Socre socre:socreList){
           System.out.print(socre.name+ " ");
       }
    }



    public static class Socre implements Comparable<Socre>{
        String name;
        Map<String,Integer> socre=new HashMap<>();

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Map<String, Integer> getSocre() {
            return socre;
        }

        public void setSocre(Map<String, Integer> socre) {
            this.socre = socre;
        }

        public Socre(){
        };

        @Override
        public int compareTo(Socre other) {
            int compareResult = other.socre.get(type).compareTo(this.socre.get(type));
            if (compareResult != 0) {
                return compareResult;
            } else {
                return this.name.compareTo(other.name);
            }
        }


    }
}
